﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _04_自定义定时器实现
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Timer timer = null;
        MyTimer myTimer = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 内置定时器
            int h = 0;
            int m = 0;
            int s = 0;

            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += (_, __) =>
            {
                s++;
                if (s >= 60)
                {
                    m++;
                    s = 0;
                }
                if (m >= 60)
                {
                    h++;
                    m = 0;
                }
                label2.Text = $"{h}:{m}:{s}";
            };
            //timer.Start();
            //timer.Stop(); // 停止计时方法

            #endregion

            #region 自定义的定时器
            int h1 = 0;
            int m1 = 0;
            int s1 = 0;
            myTimer = new MyTimer();
            myTimer.Interval = 1000;
            myTimer.Tick += () =>
            {
                //MessageBox.Show("111111111111111111111");
                s1++;
                if (s1 >= 60)
                {
                    m1++;
                    s1 = 0;
                }
                if (m1 >= 60)
                {
                    h1++;
                    m1 = 0;
                }
                label3.Text = $"{h1}:{m1}:{s1}";
            };
            //myTimer.Start();

            // 当我们创建的空间添加到窗体中 控件中的Invoke 方法就可以执行了 
            // 控件没有添加到窗体中 是不会更新的 
            this.Controls.Add(myTimer);
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
            myTimer.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer.Stop();
            myTimer.Stop();

        }
    }
}
